﻿using System;
using System.Linq;
using System.Web.Mvc;
using AdventureWorks.Entities;
using AdventureWorksData.DAO;
using AdventureWorks.Business;
using System.Collections.Generic;

namespace AdventureWorks.View.Controllers
{ 
    public class CustomerController : Controller
    {

		private readonly CustomerBusiness _buzz;
		public IEnumerable<Customer> Customers { get; set; }

		public CustomerController()
		{
			_buzz = new CustomerBusiness();
		}

		public CustomerController(IUnitOfWork entityDb)
		{
			_buzz = new CustomerBusiness(entityDb);
		}

		public ViewResult Index(String sortOrder, String currentFilter, String searchString, int? page)
		{
			Customers = _buzz.GetList(sortOrder, searchString);
			DefineViewBagSortVariables(sortOrder);

			if (Request != null)
			{
				if (Request.HttpMethod == "GET")
					searchString = currentFilter;
				else
					page = 0;
			}
    		ViewBag.CurrentFilter = searchString;

			ViewBag.PageSize = int.Parse(SystemResources.PageSize);
			ViewBag.PageIndex = page ?? 0;

			return View(Customers);
        }

		private void DefineViewBagSortVariables(String sortOrder)
		{
			ViewBag.NameSortParam = String.IsNullOrEmpty(sortOrder) ? "Name Desc" : "";
			ViewBag.DateSortParam = sortOrder == "Date" ? "Date Desc" : "Date";
			ViewBag.CompanySortParam = sortOrder == "Company" ? "Company Desc" : "Company";
			ViewBag.CurrentSort = sortOrder;
		}

        public ViewResult Details(int id)
        {
			Customer customer = _buzz.Get(id);
			return View(customer);
        }

        public ActionResult Create()
        {
			PopulateTitleDropDownList();
            return View();
        } 

        [HttpPost]
		public ActionResult Create(Customer customer, String titles)
        {
			PopulateTitleDropDownList();
            if (ModelState.IsValid)
            {
				customer.Title = titles;
				_buzz.Insert(customer);
				_buzz.Save();
                return RedirectToAction("Index");  
            }

            return View(customer);
        }
        
        public ActionResult Edit(int id)
        {
			Customer customer = _buzz.Get(id);
			PopulateTitleDropDownList(customer.Title);
			return View(customer);
        }

        [HttpPost]
        public ActionResult Edit(Customer customer, String titles)
        {
				if (ModelState.IsValid)
				{
					customer.Title = titles;
					customer.ModifiedDate = DateTime.Now;
					_buzz.Update(customer);
					_buzz.Save();
					return RedirectToAction("Index");
				}
			PopulateTitleDropDownList(customer.Title);
			return View(customer);
        }

		public void PopulateTitleDropDownList(object selectedTitle = null)
		{
			var titles = _buzz.GetList(String.Empty, String.Empty)
				.Where(t => t.Title != null)
				.GroupBy(t => t.Title);

			ViewBag.Titles = new SelectList(titles, "Key", "Key", selectedTitle);
		}

        public ActionResult Delete(int id)
        {
            Customer customer = _buzz.Get(id);
            return View(customer);
        }

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
			_buzz.Delete(id);
			_buzz.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _buzz.Dispose();
            base.Dispose(disposing);
        }
    }
}